using System.Collections;
using System.Collections.Generic;
using Game;
using Script.Structs.Pawn;
using UnityEngine;

///<summary>
///负责处理游戏中所有的DamageInfo
///</summary>
public class DamageManager : MonoBehaviour{
    private List<DamageInfo> damageInfos = new List<DamageInfo>();

    private void FixedUpdate() {
        // int i = 0;
        // while( i < damageInfos.Count ){
        //     DealWithDamage(damageInfos[i]);
        //     damageInfos.RemoveAt(0);
        // }
    }
    
    ///<summary>
    ///处理DamageInfo的流程，也就是整个游戏的伤害流程
    ///<param name="dInfo">要处理的damageInfo</param>
    ///<retrun>处理完之后返回出一个damageInfo，依照这个，给对应角色扣血处理</return>
    ///</summary>
    private void DealWithDamage(DamageInfo dInfo){
        //如果目标已经挂了，就直接return了
        if (!dInfo.defender) return;
        PawnState defenderPawnState = dInfo.defender.GetComponent<PawnState>();
        if (!defenderPawnState) return;
        // PawnState attackerPawnState = null;
        if (defenderPawnState.dead == true) 
            return;
        //先走一遍所有攻击者的onHit
        // if (dInfo.attacker){
        //     attackerPawnState = dInfo.attacker.GetComponent<PawnState>();
        //     for (int i = 0; i < attackerPawnState.buffs.Count; i++){
        //         if (attackerPawnState.buffs[i].model.onHit != null){
        //             attackerPawnState.buffs[i].model.onHit(attackerPawnState.buffs[i], ref dInfo, dInfo.defender);
        //         }
        //     }
        // }
        //然后走一遍挨打者的beHurt
        // for (int i = 0; i < defenderPawnState.buffs.Count; i++){
        //     if (defenderPawnState.buffs[i].model.onBeHurt != null){
        //        defenderPawnState.buffs[i].model.onBeHurt(defenderPawnState.buffs[i], ref dInfo, dInfo.attacker);
        //     }
        // }
        if (defenderPawnState.CanBeKilledByDamageInfo(dInfo) == true){
            //如果角色可能被杀死，就会走OnKill和OnBeKilled，这个游戏里面没有免死金牌之类的技能，所以只要判断一次就好
            // if (attackerPawnState != null){
            //     for (int i = 0; i < attackerPawnState.buffs.Count; i++){
            //         if (attackerPawnState.buffs[i].model.onKill != null){
            //             attackerPawnState.buffs[i].model.onKill(attackerPawnState.buffs[i], dInfo, dInfo.defender);
            //         }
            //     }
            // }
            // for (int i = 0; i < defenderPawnState.buffs.Count; i++){
            //     if (defenderPawnState.buffs[i].model.onBeKilled != null){
            //         defenderPawnState.buffs[i].model.onBeKilled(defenderPawnState.buffs[i], dInfo, dInfo.attacker);
            //     }
            // }
        }
        //最后根据结果处理：如果是治疗或者角色非无敌，才会对血量进行调整。
        bool isHeal = dInfo.isHeal();
        int dVal = dInfo.DamageValue(isHeal);
        if (isHeal == true || defenderPawnState.immuneTime <= 0){
            //播放受伤动画
            // if (dInfo.requireDoHurt() == true && defenderPawnState.CanBeKilledByDamageInfo(dInfo) == false){
            //     UnitAnim ua = defenderPawnState.GetComponent<UnitAnim>();
            //     if (ua) ua.Play("Hurt");
            // }
            defenderPawnState.ModResource(new PawnResource(
                -dVal
            ));
            Debug.Log(-dVal);
            //按游戏设计的规则跳数字，如果要有暴击，也可以丢在策划脚本函数（lua可以返回多参数）也可以随便怎么滴
            // SceneVariants.PopUpNumberOnCharacter(dInfo.defender, Mathf.Abs(dVal), isHeal);
            SceneVariants.NotifyMessageEvent(EventEnum.UpdatePawnState,dInfo.defender);
        }

        //伤害流程走完，添加buff
        // for (int i = 0; i < dInfo.addBuffs.Count; i++){
        //     GameObject toCha = dInfo.addBuffs[i].target;
        //     PawnState toPawnState = toCha.Equals(dInfo.attacker) ? attackerPawnState : defenderPawnState;
        //
        //     if (toPawnState != null && toPawnState.dead == false){
        //         toPawnState.AddBuff(dInfo.addBuffs[i]);
        //     }
        // }
        
    }
        
    ///<summary>
    ///处理DamageInfo 返回处理结果
    ///<param name="dInfo">要处理的damageInfo</param>
    ///<retrun>处理完之后返回出一个damageInfo，依照这个，给对应角色扣血处理</return>
    ///</summary>
    private void Damage(DamageInfo dInfo){
        //如果目标已经挂了，就直接return了
        if (!dInfo.defender) return;
        PawnState defenderPawnState = dInfo.defender.GetComponent<PawnState>();
        if (!defenderPawnState) return;
        // PawnState attackerPawnState = null;
        if (defenderPawnState.dead == true) 
            return;
        //先走一遍所有攻击者的onHit
        // if (dInfo.attacker){
        //     attackerPawnState = dInfo.attacker.GetComponent<PawnState>();
        //     for (int i = 0; i < attackerPawnState.buffs.Count; i++){
        //         if (attackerPawnState.buffs[i].model.onHit != null){
        //             attackerPawnState.buffs[i].model.onHit(attackerPawnState.buffs[i], ref dInfo, dInfo.defender);
        //         }
        //     }
        // }
        //然后走一遍挨打者的beHurt
        // for (int i = 0; i < defenderPawnState.buffs.Count; i++){
        //     if (defenderPawnState.buffs[i].model.onBeHurt != null){
        //        defenderPawnState.buffs[i].model.onBeHurt(defenderPawnState.buffs[i], ref dInfo, dInfo.attacker);
        //     }
        // }
        if (defenderPawnState.CanBeKilledByDamageInfo(dInfo) == true){
            //如果角色可能被杀死，就会走OnKill和OnBeKilled，这个游戏里面没有免死金牌之类的技能，所以只要判断一次就好
            // if (attackerPawnState != null){
            //     for (int i = 0; i < attackerPawnState.buffs.Count; i++){
            //         if (attackerPawnState.buffs[i].model.onKill != null){
            //             attackerPawnState.buffs[i].model.onKill(attackerPawnState.buffs[i], dInfo, dInfo.defender);
            //         }
            //     }
            // }
            // for (int i = 0; i < defenderPawnState.buffs.Count; i++){
            //     if (defenderPawnState.buffs[i].model.onBeKilled != null){
            //         defenderPawnState.buffs[i].model.onBeKilled(defenderPawnState.buffs[i], dInfo, dInfo.attacker);
            //     }
            // }
        }
        //最后根据结果处理：如果是治疗或者角色非无敌，才会对血量进行调整。
        bool isHeal = dInfo.isHeal();
        int dVal = dInfo.DamageValue(isHeal);
        if (isHeal == true || defenderPawnState.immuneTime <= 0){
            //播放受伤动画
            // if (dInfo.requireDoHurt() == true && defenderPawnState.CanBeKilledByDamageInfo(dInfo) == false){
            //     UnitAnim ua = defenderPawnState.GetComponent<UnitAnim>();
            //     if (ua) ua.Play("Hurt");
            // }
            defenderPawnState.ModResource(new PawnResource(
                -dVal
            ));
            Debug.Log(-dVal);
            //按游戏设计的规则跳数字，如果要有暴击，也可以丢在策划脚本函数（lua可以返回多参数）也可以随便怎么滴
            // SceneVariants.PopUpNumberOnCharacter(dInfo.defender, Mathf.Abs(dVal), isHeal);
            SceneVariants.NotifyMessageEvent(EventEnum.UpdatePawnState,dInfo.defender);
        }

        //伤害流程走完，添加buff
        // for (int i = 0; i < dInfo.addBuffs.Count; i++){
        //     GameObject toCha = dInfo.addBuffs[i].target;
        //     PawnState toPawnState = toCha.Equals(dInfo.attacker) ? attackerPawnState : defenderPawnState;
        //
        //     if (toPawnState != null && toPawnState.dead == false){
        //         toPawnState.AddBuff(dInfo.addBuffs[i]);
        //     }
        // }
        
    }

    ///<summary>
    ///添加一个damageInfo
    ///<param name="attacker">攻击者，可以为null</param>
    ///<param name="target">挨打对象</param>
    ///<param name="damage">基础伤害值</param>
    ///<param name="criticalRate">暴击率，0-1</param>
    ///<param name="tags">伤害信息类型</param>
    ///</summary>
    public void DoDamage(GameObject attacker, GameObject target, Damage damage, float criticalRate, DamageInfoTag[] tags){
        // this.damageInfos.Add(new DamageInfo(
        //     attacker, target, damage, criticalRate, tags
        // ));
        DealWithDamage(new DamageInfo(
            attacker, target, damage, criticalRate, tags
        ));
    }
}
